default_platform(:ios)

platform :ios do

  ########################--------------------------------########################
  ########################--------- SETUP LANES ----------########################
  ########################--------------------------------########################

  # Environment Setup
  ## Setup local environment configuraiton. If running on a CI machine we need to create a keychain to store the 
  ## distribution certificates
  private_lane :setup_environment do
    if is_ci
      create_keychain(
        name: ENV["KEYCHAIN_NAME"],
        password: ENV["KEYCHAIN_PASSWORD"],
        default_keychain: true,
        unlock: true,
        timeout: 3600,
        lock_when_sleeps: false
      )
    end
  end

  # Provisions and Certificates Setup
  ## Download and setup all necessary provisions and certificates (Dev & Dist)
  desc "Setup Development & Distribution profiles and certificates in the local machine"
  lane :setup_provisions do
    setup_environment

    # Install Distribution Provisions and Certificates
    match(
      app_identifier: "com.olmps.memoClient",
      type: "appstore",
      readonly: true,
      keychain_name: ENV["KEYCHAIN_NAME"],
      keychain_password: ENV["KEYCHAIN_PASSWORD"],
      verbose: true,
    )

    # Install Development Provisions and Certificates
    match(
      app_identifier: "com.olmps.memoClient",
      type: "development",
      readonly: true,
      keychain_name: ENV["KEYCHAIN_NAME"],
      keychain_password: ENV["KEYCHAIN_PASSWORD"],
      verbose: true,
    )
  end

  ########################--------------------------------########################
  ########################-------- RELEASE LANE ----------########################
  ########################--------------------------------########################

  # Release a new version to Testflight
  #
  # It assumes that the app version is correctly set.
  # The lane increments the build number if necessary
  desc "Release app to Testflight"
  lane :release do
    api_key = app_store_connect_api_key(
      key_id: ENV["APP_STORE_API_KEY_ID"],
      issuer_id: ENV["APP_STORE_API_ISSUER_ID"],
      key_content: ENV["APP_STORE_API_KEY"],
      in_house: false,
    )
    
    # Setup provision profiles
    setup_provisions

    # Clean
    sh("flutter", "clean")

    # Pub get
    sh("flutter", "pub", "get")

    # Increment the build number (use the latest build number for this version + 1)
    increment_build_number(
      build_number: latest_testflight_build_number(
        initial_build_number: 0, 
        api_key: api_key, 
        version: get_version_number
      ) + 1,
      xcodeproj: "Runner.xcodeproj",
    )

    # Run flutter build with release mode
    sh("flutter", "build", "ipa", "--release", "--dart-define=ENV=PROD", "--export-options-plist=ios/exportOptions.plist")
    
    # Zip dSYMs
    sh(
      "zip", 
      "/Users/runner/work/memo/memo/build/ios/archive/Runner.xcarchive/dSYMs.zip", 
      "/Users/runner/work/memo/memo/build/ios/archive/Runner.xcarchive/dSYMs"
    )

    # Upload dSYM files to Firebae Crashlytics
    upload_symbols_to_crashlytics(
      app_id: ENV["FIREBASE_APP_ID"],
      dsym_path: "/Users/runner/work/memo/memo/build/ios/archive/Runner.xcarchive/dSYMs.zip",
      binary_path: "/Users/runner/work/memo/memo/ios/Pods/FirebaseCrashlytics/upload-symbols",
    )

    # Upload to Testflight
    changelog = File.read("Changelog.txt")

    upload_to_testflight(
      api_key: api_key,
      distribute_external: true,
      groups: [
        'External Testers'
      ],
      changelog: changelog,
      ipa: '../build/ios/ipa/Memo.ipa'
    )
  end

  # Release a new version to Firebase Distribution
  #
  # It assumes that the app version is correctly set.
  desc "Release app to Firebase Distribution"
  lane :firebase do
    # Clean
    sh("flutter", "clean")

    # Pub get
    sh("flutter", "pub", "get")

    # Run flutter build with release mode
    sh("flutter", "build", "ipa", "--dart-define=ENV=DEV", "--export-options-plist=ios/exportOptions-dev.plist")

    sh("zip", "../../build/ios/archive/Runner.xcarchive/dSYMs.zip", "../../build/ios/archive/Runner.xcarchive/dSYMs")

    # Upload dSYM files to Firebae Crashlytics
    upload_symbols_to_crashlytics(
      app_id: ENV["MEMO_IOS_FIREBASE_APP_ID"],
      dsym_path: "../build/ios/archive/Runner.xcarchive/dSYMs.zip",
      gsp_path: "./Runner/GoogleService-Info.plist",
      binary_path: "Pods/FirebaseCrashlytics/upload-symbols",
    )

    firebase_app_distribution(
      ipa_path: '../build/ios/ipa/Memo.ipa',
      app: ENV["MEMO_IOS_FIREBASE_APP_ID"],
      googleservice_info_plist_path: "./Runner/GoogleService-Info.plist",
      groups: "internal-testers",
      service_credentials_file: "../firebase_distribution_service_account.json"
    )
  end
end
